package com.linngdu664.bettersnowballfight.mixin;

import com.linngdu664.bettersnowballfight.Register;
import com.mojang.authlib.GameProfile;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.MathHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(AbstractClientPlayerEntity.class)
public abstract class AbstractClientPlayerEntityMixin extends PlayerEntity {
    public final ClientWorld clientWorld;

    public AbstractClientPlayerEntityMixin(ClientWorld world, GameProfile profile) {
        super(world, world.getSpawnPos(), world.getSpawnAngle(), profile);
        this.clientWorld = world;
    }

    @Inject(method = "getFovMultiplier", at = @At("HEAD"), cancellable = true)
    private void getFovMultiplier(CallbackInfoReturnable<Float> info) {
        ItemStack itemstack = this.getActiveItem();
        float f = 1.0F;
        if (itemstack.isOf(Register.SNOWBALL_CANNON) || itemstack.isOf(Register.FREEZING_SNOWBALL_CANNON) || itemstack.isOf(Register.POWERFUL_SNOWBALL_CANNON)) {
            if (this.getAbilities().flying) {
                f *= 1.1F;
            }
            f *= ((float)this.getAttributeValue(EntityAttributes.GENERIC_MOVEMENT_SPEED) / this.getAbilities().getWalkSpeed() + 1.0F) / 2.0F;
            if (this.getAbilities().getWalkSpeed() == 0.0F || Float.isNaN(f) || Float.isInfinite(f)) {
                f = 1.0F;
            }
            if (this.isUsingItem()) {
                int i = this.getItemUseTime();
                float f1 = (float) i / 20.0F;
                if (f1 > 1.0F) {
                    f1 = 1.0F;
                } else {
                    f1 *= f1;
                }
                if (itemstack.isOf(Register.POWERFUL_SNOWBALL_CANNON)) {
                    f *= 1.0F - f1 * 0.5F;
                } else {
                    f *= 1.0F - f1 * 0.3F;
                }
            }
            info.setReturnValue(MathHelper.lerp(MinecraftClient.getInstance().options.fovEffectScale, 1.0F, f));
        }
    }
}
